import axios from 'axios'
axios.defaults.withCredentials = true
// const urlNavtive = 'http://localhost:3000'
const urlNavtive = 'https://r.leaguenewway.com'

export function login(user: string, password: string) {
  return axios({
    method: 'post',
    url: `${urlNavtive}/admin/login`,
    data: {
      user,
      password
    },
    responseType: 'json'
  })
}

export function cookieLogin() {
  return axios({
    method: 'get',
    url: `${urlNavtive}/admin/cookieLogin`,
    responseType: 'json'
  })
}

export function loginOut() {
  return axios({
    method: 'post',
    url: `${urlNavtive}/admin/loginOut`,
    responseType: 'json'
  })
}

export interface siperType {
  [key: string]: any
  _id: string
  vod_name: string
  vod_info: string
  vod_index: number
  vod_img_small: string
  vod_img: string
  type_id: string
}

export function editSwiper(typeid: string, list: siperType[]) {
  return axios({
    method: 'post',
    url: `${urlNavtive}/admin/editSwiper`,
    data: {
      typeid,
      list
    },
    responseType: 'json'
  })
}

import { type typeCalss } from './video'

export function editClass(class_: typeCalss[]) {
  return axios({
    method: 'post',
    url: `${urlNavtive}/admin/editClass`,
    data: {
      classList: class_
    },
    responseType: 'json'
  })
}

export function reptileInfo() {
  return axios({
    method: 'get',
    url: `${urlNavtive}/reptile`,
    responseType: 'json'
  })
}

export function reptileSet(d: number, h: number, username: string) {
  return axios({
    method: 'post',
    url: `${urlNavtive}/reptile/set`,
    data: {
      d,
      h,
      username
    },
    responseType: 'json'
  })
}

export function getUsers(page: number, count: number, user?: string) {
  return axios({
    method: 'get',
    url: `${urlNavtive}/admin/getUsers`,
    params: {
      page,
      count,
      user
    },
    responseType: 'json'
  })
}

export function configSet(key: string, value: Object | string) {
  return axios({
    method: 'post',
    url: `${urlNavtive}/admin/configSet`,
    data: {
      key,
      value
    },
    responseType: 'json'
  })
}

export function siteReport(date: string) {
  return axios({
    method: 'get',
    url: `${urlNavtive}/admin/siteReport`,
    params: {
      date
    },
    responseType: 'json'
  })
}

export function apiReport(date: string) {
  return axios({
    method: 'get',
    url: `${urlNavtive}/admin/apiReport`,
    params: {
      date
    },
    responseType: 'json'
  })
}

axios.interceptors.response.use(
  function (response) {
    if (response.request.responseURL.indexOf('/admin/cookieLogin') == -1 && response.data.code == 303) {
      //token失效
      window.location.href = '/'
    }
    return response
  },
  function (error) {
    console.log({ error })
    return Promise.reject(error)
  }
)
